package com.dennytech.tac.utils.db;

import java.io.File;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.graphics.BitmapFactory;

import com.dennytech.tac.model.CityIntro;

public class CityIntroDBHelper extends DBHelper<CityIntro>{

	private static CityIntroDBHelper instance;

	public static CityIntroDBHelper instance() {
		return instance;
	}

	public static void init(Context c) {
		if (instance == null) {
			instance = new CityIntroDBHelper(c);
		}
	}

	private CityIntroDBHelper(Context c) {
		super(c, "cityintro");
	}

	public CityIntro getCityIntro(String name) {
		CityIntro cityIntro = new CityIntro();
		try {
			String query = "SELECT * FROM " + tableName + " WHERE NAME='"
					+ name + "';";
			Cursor c = db.rawQuery(query, null);
			if (c.moveToFirst()) {
				cityIntro.setName(c.getString(1));

				cityIntro.setShortName(c.getString(2));

				byte[] bigicon = c.getBlob(3);
				cityIntro.setBigIcon(BitmapFactory.decodeByteArray(bigicon, 0,
						bigicon.length));

				cityIntro.setReviewerName(c.getString(4));

				byte[] reviewericon = c.getBlob(5);
				cityIntro.setReviewerIcon(BitmapFactory.decodeByteArray(
						reviewericon, 0, reviewericon.length));

				cityIntro.setReview(c.getString(6));

				c.close();
			} else {
				c.close();
				return null;
			}
		} catch (Exception e) {
			return null;
		}
		return cityIntro;
	}

	@Override
	public String dbName() {
		return "city.db";
	}

	@Override
	public File dbDir(Context c) {
		return c.getCacheDir();
	}

	@Override
	public List<CityIntro> query() {
		return null;
	}

}
